package com.gm.common.controller.action;

 
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; 
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
 
/**
 *  
 * 
 * @version $Revision$
 * 
 */
public abstract class CommonAction extends ActionSupport {
	
	public static final String JSON = "json";
	public static final String HTML = "html";
	
	private static final long serialVersionUID = -5289611895238514280L;

	private static String APP_DOMAIN;
	private static String CART_DOMAIN;

	protected String writeAjaxResponse(String ajaxString) {
		try {
			getResponse().setContentType("text/html");
			getResponse().setCharacterEncoding("utf-8");
			PrintWriter out = getResponse().getWriter();
			out.write(ajaxString);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			return null;
		}
	}
	public String getAppDomain(){
		return APP_DOMAIN;
	}
	public String getCartDomain(){
		return CART_DOMAIN;
	}
	
	protected String dataType;
	
	 
	/**
	 * UserSessionObj
	 * 
	 * @return UserSessionObj
	 */
	public String getUserSessionInfo() {
		return (String) ServletActionContext.getRequest().getSession()
				.getAttribute("username");
	}

	/**
	 * ����UserSessionObj���û���Session�У���ͬ��OnlineUserMap��
	 * 
	 * @param sessionObj
	 *            Ҫ�����UserSessionObj
	 */
	public void setUserSessionInfo(String username) {
		ServletActionContext.getRequest().getSession().setAttribute("username",
				username);
	}

	/**
	 * ����û���Session�е�UserSessionObj��
	 * 
	 */
	public void clearUserSessionInfo() {
		ServletActionContext.getRequest().getSession().setAttribute("username",
				null);
	}

	 

	/**
	 * ��̨����û���Session�е�UserSessionObj��
	 * 
	 */
	public void clearAdminSessionInfo() {
		ServletActionContext.getRequest().getSession().setAttribute(
				"admin", null);
		getSession().invalidate();
	}


	/**
	 * ����ֵ��cookies��
	 * 
	 * @param key
	 *            ��
	 * 
	 * @param value
	 *            ֵ
	 */
	public void addCookie(String key, String value) {
		Cookie cookie = new Cookie(key, value);
		cookie.setPath("/");// ���Ҫ����
		// �����õĻ�����cookies��д��Ӳ��,����д���ڴ�,ֻ�ڵ�ǰҳ������,����Ϊ��λ
		cookie.setMaxAge(365 * 24 * 60 * 60);
		this.getResponse().addCookie(cookie);
	}

	public void addCookie(String key, String value, int time) {
		Cookie cookie = new Cookie(key, value);
		cookie.setPath("/");// ���Ҫ����
		// �����õĻ�����cookies��д��Ӳ��,����д���ڴ�,ֻ�ڵ�ǰҳ������,����Ϊ��λ
		cookie.setMaxAge(time);
		this.getResponse().addCookie(cookie);
	}

	public void addCookie(String key, String value, String domain) {
		Cookie cookie = new Cookie(key, value);
		cookie.setPath("/");// ���Ҫ����
		// �����õĻ�����cookies��д��Ӳ��,����д���ڴ�,ֻ�ڵ�ǰҳ������,����Ϊ��λ
		cookie.setMaxAge(365 * 24 * 60 * 60);
		cookie.setDomain(domain);
		this.getResponse().addCookie(cookie);
	}

	/**
	 * ��ݼ��ȡ��Ӧ��cookie����
	 * 
	 * @param key
	 *            ��Ӧ��key
	 * 
	 * @return key��Ӧ��cookieֵ
	 */
	public Cookie getCookie(String key) {
		Cookie[] cookies = this.getRequest().getCookies();
		if (cookies == null || cookies.length < 1)
			return null;
		for (Cookie temp : cookies) {
			if (temp.getName().equals(key)) {
				return temp;
			}
		}
		return null;
	}
	

	/**
	 * ���cookie��Ӧ��ֵ
	 * 
	 * @param key
	 *            ��Ӧ�ļ�
	 * 
	 * @return ����ֵ
	 */
	public String getCookieValue(String key) {
		Cookie cookie = this.getCookie(key);
		if (cookie != null) {
			try {
				return URLDecoder.decode(cookie.getValue(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public void clearCookie(String key) {
		Cookie cookie = new Cookie(key, null);
		cookie.setMaxAge(0);
		cookie.setPath("/");
		this.getResponse().addCookie(cookie);
	}
	

	/**
	 * ���session�е�user
	 * 
	 * @return
	 */
	/*public User getUser() {
		UserSessionObj userObj = getUserSessionInfo();
		if (userObj == null)
			return null;

		User user = userObj.getUser();
		if (user == null)
			return null;

		return user;

	}
	*/

	
	/**
	 * ��ȡ����Ա��̨����Ա����
	 * 
	 * @return
	 */
	protected String getAdminSessionOperator() {
		try {
			return getSession().getAttribute("username").toString();
		} catch (Exception ex) {
			return "";
		}
	}
	
 
	

	protected void setLastQueryString() {
		String requesturl = this.getRequest().getQueryString();
		this.getSession().setAttribute("lasturl", requesturl);
	}

	protected String getLastQueryString() {
		Object lastRequesturl = this.getSession().getAttribute("lasturl");
		if (lastRequesturl != null)
			return (String) lastRequesturl;
		return "";
	}

	protected void removeLastQueryString() {
		this.getSession().removeAttribute("lasturl");
	}
	
	public String getDataType() {
		return dataType;
	}
	public void setDataType(String dataType) {
		this.dataType = dataType;
	}
	
	
	protected void writeAjaxStr(String str){
		HttpServletResponse response = this.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		try {
			PrintWriter out = response.getWriter();
			out.write(str);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
 
	
	/**
	 * �õ�HttpRequest
	 * 
	 * @return ��õ�HttpRequest
	 */
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * �õ�HttpSession
	 * 
	 * @return ��õ�HttpSession
	 */
	public HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	/**
	 * �õ�HttpResponse
	 * 
	 * @return ��õ�HttpResponse
	 */
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * �õ�������·�������磺/mylearningii
	 * 
	 * @return ������·��
	 */
	public String getContext() {
		return this.getRequest().getContextPath();
	}

	/**
	 * �õ�����ȫ·�������磺http://127.0.0.1:8080/mylearningii
	 * 
	 * @return ����ȫ·��
	 */
	public String getBasePath() {
		String path = this.getRequest().getContextPath();
		String basePath = this.getRequest().getScheme() + "://" + this.getRequest().getServerName();

		if ( this.getRequest().getServerPort() != 80 ) {
			basePath += ":" + this.getRequest().getServerPort() + path;
		} else {
			basePath += path;
		}

		return basePath;
	}
	
	/**
	 * �õ�Ӧ�õľ�Ե�ַ�����磺D:\Eclipse\workspace\CourseCreater\web\
	 * 
	 * @return Ӧ�õľ�Ե�ַ
	 */
	public String getRealPath() {
		return ServletActionContext.getServletContext().getRealPath("/");
	}

	/**
	 * ����û�����ʵIP
	 * 
	 * @return �û�����ʵIP
	 */
	public String getRealIP() {
		String ip = getRequest().getHeader("x-forwarded-for");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = getRequest().getHeader("Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = getRequest().getHeader("WL-Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = getRequest().getRemoteAddr();
		}
		
		return ip;
	}
	
	/**
	 * ��������referer
	 * 
	 * @return referer
	 */
	public String getReferer() {
		String referer = getRequest().getHeader("referer");
		if (referer != null && !referer.equalsIgnoreCase("")) {
			referer = referer.substring(0, referer.lastIndexOf("/")+1);
		} else {
			referer = "";
		}
		
		return referer;
	}
	 
}